﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Presentation.PropertyEditing;
using System.Activities.Statements;
using System.Collections.Generic;
using System.ComponentModel;
using Core = Vision.Core;

namespace Vision.Core.ActivityComponents.IO.Vfs
{
    /// <summary>
    /// 
    /// </summary>
    public class VfsFileSave : NativeActivity
    {
        /// <summary>
        /// 
        /// </summary>
        public InArgument<string> CatalogConnection { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public InArgument<string> Key { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public InArgument<string> Name { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public InArgument<string> Description { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public InArgument<int> FolderId { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public InArgument<byte[]> FileData { get; set; }
        
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(NativeActivityContext context)
        {
            DateTime timeStamp = DateTime.Now;
            Entities.Filing.Document document = new Entities.Filing.Document();
            document.Key = Guid.Parse(context.GetValue(this.Key));
            document.Name = context.GetValue(this.Name);
            document.Description = context.GetValue(this.Description);
            document.FolderId = context.GetValue(this.FolderId);
            document.FolderPath = string.Empty;
            document.Data = context.GetValue(this.FileData);
            document.Created = timeStamp;
            document.Modified = timeStamp;

            using (Core.Components.ActivityCatalog documentCatalog = new Core.Components.ActivityCatalog(this.CatalogConnection.Get(context))){
                documentCatalog.SaveVisionDocumentToCatalog(document);                     
            }             
        }
    }
}
